import org.apache.commons.codec.binary.Base64;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class DeWebService {

    public static void main(String[] args) throws IOException {
        String requestData = "";
        String userName = "ASADMIN";
        String password = "asadmin";
        String url = "http://ebssit.streamax.com:8006/webservices/rest/INVOKEFMSWS/INVOKEFMSWS/";
        String host = "http://ebssit.streamax.com";
        int port = 8006;

        CredentialsProvider provider = new BasicCredentialsProvider();
        provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();

        CloseableHttpResponse response = null;
        RequestConfig config = RequestConfig.custom().setSocketTimeout(5000000).setConnectTimeout(5000000).build();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(config);

        httpPost.setHeader("Context-Type","application/xml");
        httpPost.setHeader("Username", userName);
        httpPost.setHeader("Password", password);

        StringBuffer stringBuffer = new StringBuffer("");
        stringBuffer.append("<ns:GET_Input xmlns:ns=\"http://ebssit.streamax.com:8006/webservices/rest/common_service/INVOKEFMSWS/\"");
        stringBuffer.append(" xmlns:ns1=\"http://xmlns.oracle.com/apps/fnd/soaprovider/plsql/rest/common_service/header\">");
        stringBuffer.append("<ns:InputParameters><ns:P_IFACE_CODE>CUX_PO_VENDOR_Q</ns:P_IFACE_CODE>");
        stringBuffer.append("<ns:P_BATCH_NUMBER>201804160001</ns:P_BATCH_NUMBER>");
        stringBuffer.append("<ns:P_REQUEST_DATA>PHJvb3Q+PGhlYWQ+PEg+CjxTVEFSVFRJTUU+MjAxODA0MjcwMDAyPC9TVEFSVFRJTUU+CjxFTkRUSU1FPjIwMTgwNTI3MDAwMjwvRU5EVElNRT4KPE9SR19JRD44MTwvT1JHX0lEPgo8L0g+PC9oZWFkPjwvcm9vdD4=</ns:P_REQUEST_DATA>");
        stringBuffer.append("</ns:InputParameters>");
        stringBuffer.append("</ns:GET_Input>");
        requestData = String.valueOf(stringBuffer);

        final Base64 base64 = new Base64();
        System.out.println("请求参数解码为:\n" + new String (base64.decode("PHJvb3Q+PGhlYWQ+PEg+CjxTVEFSVFRJTUU+MjAxODA0MjcwMDAyPC9TVEFSVFRJTUU+CjxFTkRUSU1FPjIwMTgwNTI3MDAwMjwvRU5EVElNRT4KPE9SR19JRD44MTwvT1JHX0lEPgo8L0g+PC9oZWFkPjwvcm9vdD4="),"UTF-8"));

        StringEntity data = new StringEntity(requestData, "UTF-8");
        System.out.println("Header:  "+httpPost.getAllHeaders());
        data.setContentType("application/xml");

        httpPost.setEntity(data);
        response = httpClient.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("StatusCode : " + statusCode);
        System.out.println("Response : " + response.getEntity());
        System.out.println(EntityUtils.toString(response.getEntity(),"UTF-8"));
    }
}
